Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
@solana/spl-token
Advanced tools
@solana/spl-token is an npm package that provides a set of tools and utilities for interacting with the Solana blockchain's SPL Token program. This package allows developers to create, manage, and interact with SPL tokens, which are the standard tokens on the Solana blockchain.
Create a New Token
This feature allows you to create a new SPL token on the Solana blockchain. The code sample demonstrates how to set up a connection to the Solana network, generate keypairs for the payer, mint authority, and freeze authority, and then create a new token with specified decimal places.
const { Connection, Keypair } = require('@solana/web3.js');
const { createMint } = require('@solana/spl-token');
(async () => {
const connection = new Connection('https://api.mainnet-beta.solana.com');
const payer = Keypair.generate();
const mintAuthority = Keypair.generate();
const freezeAuthority = Keypair.generate();
const mint = await createMint(
connection,
payer,
mintAuthority.publicKey,
freezeAuthority.publicKey,
9 // Decimals
);
console.log('Created new token:', mint.toBase58());
})();
Create a Token Account
This feature allows you to create a new token account for a specific SPL token. The code sample demonstrates how to set up a connection, generate keypairs for the payer, mint, and owner, and then create a new token account associated with the specified mint and owner.
const { Connection, Keypair } = require('@solana/web3.js');
const { createAccount } = require('@solana/spl-token');
(async () => {
const connection = new Connection('https://api.mainnet-beta.solana.com');
const payer = Keypair.generate();
const mint = Keypair.generate(); // Assume this is your token mint
const owner = Keypair.generate();
const tokenAccount = await createAccount(
connection,
payer,
mint.publicKey,
owner.publicKey
);
console.log('Created new token account:', tokenAccount.toBase58());
})();
Mint New Tokens
This feature allows you to mint new tokens to a specified token account. The code sample demonstrates how to set up a connection, generate keypairs for the payer, mint, destination account, and mint authority, and then mint a specified amount of tokens to the destination account.
const { Connection, Keypair } = require('@solana/web3.js');
const { mintTo } = require('@solana/spl-token');
(async () => {
const connection = new Connection('https://api.mainnet-beta.solana.com');
const payer = Keypair.generate();
const mint = Keypair.generate(); // Assume this is your token mint
const destination = Keypair.generate(); // Assume this is your token account
const mintAuthority = Keypair.generate();
await mintTo(
connection,
payer,
mint.publicKey,
destination.publicKey,
mintAuthority,
1000000 // Amount to mint
);
console.log('Minted new tokens');
})();
Transfer Tokens
This feature allows you to transfer tokens from one token account to another. The code sample demonstrates how to set up a connection, generate keypairs for the payer, source account, destination account, and owner, and then transfer a specified amount of tokens from the source account to the destination account.
const { Connection, Keypair } = require('@solana/web3.js');
const { transfer } = require('@solana/spl-token');
(async () => {
const connection = new Connection('https://api.mainnet-beta.solana.com');
const payer = Keypair.generate();
const source = Keypair.generate(); // Assume this is your source token account
const destination = Keypair.generate(); // Assume this is your destination token account
const owner = Keypair.generate();
await transfer(
connection,
payer,
source.publicKey,
destination.publicKey,
owner,
500000 // Amount to transfer
);
console.log('Transferred tokens');
})();
@solana/web3.js is a JavaScript API for interacting with the Solana blockchain. While it provides lower-level functionalities for interacting with the Solana network, it does not specifically focus on SPL tokens like @solana/spl-token does. However, it is often used in conjunction with @solana/spl-token for broader Solana development.
@project-serum/serum is a package for interacting with the Serum decentralized exchange on the Solana blockchain. While it provides functionalities for trading and market-making, it does not focus on the creation and management of SPL tokens like @solana/spl-token.
@solana/spl-governance is a package for creating and managing governance structures on the Solana blockchain. While it provides tools for decentralized governance, it does not offer the same token management functionalities as @solana/spl-token.
@solana/spl-token
A TypeScript library for interacting with the SPL Token and Token-2022 programs.
Please ask questions in the Solana Stack Exchange: https://solana.stackexchange.com/
If you've found a bug or you'd like to request a feature, please open an issue.
Please see upgrading from 0.1.x.
npm install --save @solana/spl-token @solana/web3.js
OR
yarn add @solana/spl-token @solana/web3.js
git clone https://github.com/solana-labs/solana-program-library.git
cd solana-program-library/token/js
npm install
npm run build
npm run test:build-programs
npm run test
npm run example
There are no breaking changes from 0.2.0, only new functionality for Token-2022.
When upgrading from spl-token 0.1.x, you may see the following error in your code:
import {TOKEN_PROGRAM_ID, Token, AccountLayout} from '@solana/spl-token';
^^^^^
SyntaxError: The requested module '@solana/spl-token' does not provide an export named 'Token'
The @solana/spl-token
library as of version 0.2.0 does not have the Token
class. Instead the actions are split up and exported separately.
To use the old version, install it with:
npm install @solana/spl-token@0.1.8
Otherwise you can find documentation on how to use new versions on the SPL docs or Solana Cookbook.
FAQs
SPL Token Program JS API
The npm package @solana/spl-token receives a total of 144,629 weekly downloads. As such, @solana/spl-token popularity was classified as popular.
We found that @solana/spl-token demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 14 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.